package com.baor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.OrderDao;
import com.baor.domain.GoodsVo;
import com.baor.domain.OrderVo;
import com.baor.domain.field.OrderStatusField;
import com.baor.enums.FuncEnum;
import com.baor.service.OrdersService;
import com.baor.utils.AjaxResult;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrderDao orderDao;


    private Page<OrderVo> getPage(Map<String,Object> map){
        if ( map.get("pageIndex") == null || "".equals(map.get("pageIndex")) )
            map.put("pageIndex",1);
        if ( map.get("pageSize") == null || "".equals(map.get("pageSize")) )
            map.put("pageSize",10);
        Long pageIndex = Long.valueOf(map.get("pageIndex").toString());
        Long pageSize = Long.valueOf(map.get("pageSize").toString());
        return new Page<>(pageIndex,pageSize);
    }


    private MPJLambdaWrapper<OrderVo> getMpj(){
        MPJLambdaWrapper<OrderVo> mpj = new MPJLambdaWrapper<>();
        mpj
                .selectAll(OrderVo.class)//不需要第二行的话,第一行一同删除,用这行代替
                .selectAs(OrderStatusField::getOrderStatusName, OrderVo::getOrderStatusId)//联表字段,(as),主表字段
                .selectAs(GoodsVo::getGoodsName,OrderVo::getGoodsId)
                .selectAs(GoodsVo::getGoodsImage,OrderVo::getGoodsImage)
                .leftJoin(GoodsVo.class,GoodsVo::getGoodsId,OrderVo::getGoodsId)
                .leftJoin(OrderStatusField.class,OrderStatusField::getOrderStatusId,OrderVo::getOrderStatusId);//联表类,(on),链表联合字段,(=),主表联合字段
        return mpj;
    }


    @Override
    public AjaxResult<?> userOrderList(Map<String, Object> map) {
        MPJLambdaWrapper<OrderVo> mpj = getMpj();
        if ( map.get("userId") == null || "".equals(map.get("userId")) )
            return AjaxResult.error("userId is null");
        if ( map.get("orderStatusId") != null && !"".equals(map.get("orderStatusId")) )
            mpj.eq(OrderVo::getOrderStatusId,map.get("orderStatusId"));
        mpj.eq(OrderVo::getUserId,map.get("userId"));
        Page<OrderVo> page = orderDao.selectPage(getPage(map), mpj);
        //-------- 结果处理 --------
        List<OrderVo> records = page.getRecords();
//        records.stream().
        long pages = page.getPages();
        //-------- 数据封装 --------
        Map<String,Object> res = new HashMap<>();
        res.put("pageCount",pages);
        res.put("list",records);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult<?> orderInfo(Map<String, Object> map) {
        MPJLambdaWrapper<OrderVo> mpj = getMpj();
        if ( map.get("userId") == null || "".equals(map.get("userId")) )
            return AjaxResult.error("userId is null");
        if ( map.get("orderId") == null || "".equals(map.get("orderId")) )
            return AjaxResult.error("orderId is null");
        if ( map.get("orderStatusId") != null && !"".equals(map.get("orderStatusId")) )
            mpj.eq(OrderVo::getOrderStatusId,map.get("orderStatusId"));
        mpj.eq(OrderVo::getUserId,map.get("userId"))
                .eq(OrderVo::getOrderId,map.get("orderId"));
        Page<OrderVo> page = orderDao.selectPage(getPage(map), mpj);
        //-------- 结果处理 --------
        List<OrderVo> records = page.getRecords();
        //-------- 数据封装 --------
        return AjaxResult.success(records.size() > 0 ? records.get(0) : null);
    }
}
